package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.dto.product.ProductDto;
import com.zhentao.entity.product.Product;
import com.zhentao.service.ProductService;
import com.zhentao.vo.common.Result;
import com.zhentao.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value="/admin/product/product")
public class ProductController {

    @Resource
    private ProductService productService ;

    /**
     * 商品列表
     * @param productDto
     * @return
     */
    @PostMapping(value = "/")
    public Result<Page<Product>> findByPage(@RequestBody ProductDto productDto) {
        Page<Product> pageInfo = productService.findByPage(productDto);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 添加商品
     * @param product
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Product product) {
        productService.saveAdd(product);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据ID查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result<Product> getById(@PathVariable Long id) {
        Product product = productService.findById(id);
        return Result.build(product , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 修改商品信息
     * @param product
     * @return
     */
    @PutMapping("/updateById")
    public Result updateById(@Parameter(name = "product", description = "请求参数实体类", required = true) @RequestBody Product product) {
        productService.updateByProduct(product);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 删除商品信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@Parameter(name = "id", description = "商品id", required = true) @PathVariable Long id) {
        productService.deleteById(id);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    /**
     * 商品审核
     * @param id
     * @param auditStatus
     * @return
     */
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return Result.build(null , ResultCodeEnum.SUCCESS);
    }

    /**
     * 商品上下架
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }
}